package com.grade.controller;

import com.grade.Beans.Student;
import com.grade.Beans.Subjects;
import com.grade.service.StudentService;
import com.grade.service.SubjectsService;
import com.grade.utils.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/subjects")
@ResponseBody
public class SubjectsController {
    @Autowired
    SubjectsService subjectsService;

    @RequestMapping("/getAllSubjects")
    public Results<List<Subjects>> findAllSubjects(@RequestParam(value = "name", defaultValue = "") String name,
                                                  @RequestParam(value = "page", defaultValue = "1") int page,
                                                  @RequestParam(value = "limit", defaultValue = "5") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "%" + name + "%");
        map.put("startpage", (page - 1) * pageSize);
        map.put("pageSize", pageSize);
        List<Subjects> subjectsList = subjectsService.findBySubjectsPage(map);
        long count = subjectsService.findBySubjects(map);
        Results<List<Subjects>> results = new Results<>("0", "查询成功", subjectsList, count);
        return results;
    }

    @RequestMapping("/addSubjects")
    public Results<Subjects> addSubjects(Subjects subjects) {
        Subjects s = subjectsService.findByName( subjects.getName());
        Results<Subjects> results = null;
        if (s != null) {
            results = new Results<>("1", "该学号已经有了");
        } else {
            int count = subjectsService.addSubjects(subjects);
            if (count == 1) {
                results = new Results<>("0", "新增成功");
            } else {
                results = new Results<>("2", "新增失败");
            }
        }
        return results;
    }

    @RequestMapping("/deleteSubjects")
    public Results<Subjects> deleteSubjects(int id) {
        int count = subjectsService.deleteSubjects(id);
        Results<Subjects> results = null;
        if (count == 1) {
            results = new Results<>("0", "删除成功");
        } else {
            results = new Results<>("1", "删除失败");
        }
        return results;
    }

    @RequestMapping("/updateSubjects")
    public Results<Subjects> updateSubjects(Subjects subjects) {
        int count = subjectsService.updateSubjects(subjects);
        Results<Subjects> results = null;
        if (count == 1) {
            results = new Results<>("0", "更新成功");
        } else {
            results = new Results<>("2", "更新失败");
        }
        return results;
    }
    @PostMapping("/subjectsSelect")
    public  Results<List<Subjects>> subjectsSelect(){
        List<Subjects> subjectsList=subjectsService.findAll();
        return new Results<>("0",null,subjectsList,null);
    }
}
